<template>
  <div>
    <el-table :data="resOrderInfo" border style="width: 100%">
      <el-table-column fixed prop="orderNo" label="订单号" width="150">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.orderNo }}</p>
          <el-input
            v-model="row.orderNo"
            placeholder="请输入内容"
            v-show="row.flag"
            size="small"
          ></el-input>
        </template>
      </el-table-column>

      <el-table-column prop="orderTime" label="下单时间" width="160">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.orderTime | formate }}</p>

          <el-date-picker
            v-show="row.flag"
            v-model="row.orderTime"
            type="date"
            placeholder="选择日期"
            size="small"
          >
          </el-date-picker>
        </template>
      </el-table-column>

      <el-table-column prop="phone" label="手机号" width="150">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.phone }}</p>
          <el-input
            v-model="row.phone"
            placeholder="请输入内容"
            v-show="row.flag"
            size="small"
          ></el-input>
        </template>
      </el-table-column>

      <el-table-column prop="consignee" label="收货人" width="120">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.consignee }}</p>
          <el-input
            v-model="row.consignee"
            placeholder="请输入内容"
            v-show="row.flag"
            size="small"
          ></el-input>
        </template>
      </el-table-column>

      <el-table-column prop="deliverAddress" label="配送地址" width="300">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.deliverAddress }}</p>
          <el-input
            v-model="row.deliverAddress"
            placeholder="请输入内容"
            v-show="row.flag"
            size="small"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="deliveryTime" label="送达时间" width="160">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.deliveryTime | formate }}</p>
          <el-date-picker
            v-show="row.flag"
            v-model="row.deliveryTime"
            type="date"
            placeholder="选择日期"
            size="small"
          >
          </el-date-picker>
        </template>
      </el-table-column>

      <el-table-column prop="remarks" label="用户备注" width="200">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.remarks }}</p>
          <el-input
            v-model="row.remarks"
            placeholder="请输入内容"
            v-show="row.flag"
            size="small"
          ></el-input>
        </template>
      </el-table-column>

      <el-table-column prop="orderAmount" label="订单金额" width="120">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.orderAmount }}</p>
          <el-input
            v-model="row.orderAmount"
            placeholder="请输入内容"
            v-show="row.flag"
            size="small"
          ></el-input>
        </template>
      </el-table-column>

      <el-table-column prop="orderState" label="订单状态" width="120">
        <template v-slot="{ row }">
          <p v-show="!row.flag">{{ row.orderState }}</p>
          <el-input
            v-model="row.orderState"
            placeholder="请输入内容"
            v-show="row.flag"
            size="small"
          ></el-input>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="100">
        <template v-slot="{ row }">
          <el-button @click="handleClick(row)" type="text" size="small"
            >查看</el-button
          >
          <el-button type="text" size="small" @click="handleEdit(row)">{{
            row.flag ? '完成' : '编辑'
          }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="收货地址" :visible.sync="dialogTableVisible">
      <ul class="order-info">
        <li><span>id:</span> {{ detailInfo.id }}</li>
        <li><span>订单号:</span> {{ detailInfo.orderNo }}</li>
        <li><span>收货人:</span> {{ detailInfo.consignee }}</li>
        <li><span>收货地址:</span> {{ detailInfo.deliverAddress }}</li>
        <li><span>送达时间:</span> {{ detailInfo.deliveryTime | formate }}</li>
        <li><span>订单数量:</span> {{ detailInfo.orderAmount }}</li>
        <li><span>订单状态:</span> {{ detailInfo.orderState }}</li>
        <li><span>发货时间:</span> {{ detailInfo.orderTime | formate }}</li>
        <li><span>手机号码:</span> {{ detailInfo.phone }}</li>
        <li><span>备注</span> {{ detailInfo.remarks }}</li>
      </ul>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment'
import { confirmMsg } from '@/util/msgTips'

export default {
  data () {
    return {
      changeTable: false, // 默认隐藏,
      dialogTableVisible: false
    }
  },
  computed: {
    orderTime (time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    renderInfo () {
      return this.resOrderInfo.slice(
        this.reqOrderInfo.currentPage - 1,
        this.reqOrderInfo.pageSize
      )
    }
  },
  props: {
    resOrderInfo: {
      type: Array,
      default: () => [],
      require: true
    },
    updateOrderInfo: {
      type: Function,
      require: true
    },
    getOrderDetailInfo: {
      type: Function,
      require: true
    },
    detailInfo: {
      type: Object,
      default: () => {},
      require: true
    },
    reqOrderInfo: {
      type: Object,
      default: () => {},
      require: true
    }
  },

  // 处理编辑 --> 表格可以修改,按钮变成完成
  methods: {
    // 编辑
    handleEdit (data) {
      data.orderTime = moment(data.orderTime).format('YYYY-MM-DD HH:mm:ss')
      data.deliveryTime = moment(data.deliveryTime).format(
        'YYYY-MM-DD HH:mm:ss'
      )
      // 编辑
      if (!data.flag) {
        // 显示
        this.$set(data, 'flag', true)
      } else {
        // 完成
        confirmMsg(
          () => {
            // 确认编辑
            // 发送请求
            this.updateOrderInfo(data)
            // 隐藏
            // this.$set(data, 'flag', false)
            data.flag = false
            console.log(data.flag, 'bbbb')
          },
          '是否确认修改!',
          () => {
            // this.$set(data, 'flag', false)
            data.flag = false
          }
        )
      }
    },

    handleClick (row) {
      this.dialogTableVisible = true
      this.getOrderDetailInfo(row.id)
    }
  },
  filters: {
    formate (date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 80px;
}

.order-info {
  li {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 60px;
    font-size: 16px;
    border-bottom: 1px solid #ccc;
    span {
      display: flex;
      width: 100%;
      font-size: 18px;
    }
  }
}
</style>
